package com.grgbanking.it.system.dao.impl;


import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.grgbanking.it.base.BaseDao;
import com.grgbanking.it.common.util.Converter;
import com.grgbanking.it.system.dao.IResourceDao;
import com.grgbanking.it.system.entity.Resource;


/**
 * 系统资源Dao
 * 
 * IResourceDao
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2014-12-11 下午3:36:35
 * 
 * @version 1.0.0
 *
 */
@Repository 
public class ResourceDao extends BaseDao<Resource, Integer> implements IResourceDao<Resource, Integer> {
    @Autowired  
    public void setMySessionFactory(SessionFactory sessionFactory){  
        super.setSessionFactory(sessionFactory);  
    }

	@Override
	public List<Resource> findChildren(Integer parentId) {
		StringBuffer hql = new StringBuffer("from Resource as r where r.state<>0 ");
		hql.append(" and r.parent=").append(parentId);
		
		return this.findByHql(hql.toString());
	}
	
	/**
	 * 获取用户所有的资源
	 * findByUser
	 * 
	 * @param username 用户ID
	 * @return 
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Resource> findByUser(String username) throws Exception {
		StringBuffer sql = new StringBuffer("select distinct r.* from resource r\n");
		sql.append("right join m_role_resource m on r.id=m.resource_id \n");
		sql.append("and m.role_id in(select u.role_id from m_user_role u where u.username='").append(username).append("')\n");
		sql.append(" where r.state<>0");
		
		return this.findBySQL(sql.toString());
	}
	
}